﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Wintellect.PowerCollections;

using DbSharp.Framework.DbObject;
using DbSharp.Framework.Storage;
using DbSharp.Framework.Storage.Export;

namespace DbSharp.Framework.DbObject
{
    public class ScalarExecutionResult
    {
        #region Private members
        private StatementArgument m_Argument;
        private XmlExportWriter m_XmlExportWriter = new XmlExportWriter();
        #endregion

        public ScalarExecutionResult(StatementArgument argument)
        {
            m_Argument=argument;
        }

        #region Public methods/properties

        public string ScalarValue { get; set; }
        public XElement ScalarValueXElement { get; set; }
        public IScalarStorage Storage { get; set; }
        public List<string> OutputFieldNameList { get; set; }
        public List<FieldMetaData> OutputFieldMetaDataQueue { get; set; }
        public string ExportedFileName { get; set; }
        public StatementArgument Argument
        {
            get { return m_Argument; }
        }
        public void ExportToXmlFile(string xmlFile, IScalarStorage storage)
        {
            m_XmlExportWriter.Export(xmlFile, storage.ScalarValueXElement, long.MaxValue);
        }

        #endregion
    }


    public class RecordsetExecutionResult
    {
        #region Private members
        private StatementArgument m_Argument;
        private XmlExportWriter m_XmlExportWriter = new XmlExportWriter();
        #endregion

        public RecordsetExecutionResult(StatementArgument argument)
        {
            m_Argument = argument;
        }

        #region Public methods/properties

        public double FetchDataMilliseconds { get; set; }
        public List<XElement> XElmtRecords { get; set; }
        public List<string> OutputFieldNameList { get; set; }
        public List<FieldMetaData> OutputFieldMetaDataQueue { get; set; }
        public IRecordsetStorage Storage { get; set; }
        public string ExportedFileName { get; set; }
        public StatementArgument Argument
        {
            get { return m_Argument; }
        }

        public void ExportToXmlFile(string xmlFile, IRecordsetStorage Storage, long exportRecordCount)
        {
            m_XmlExportWriter.Export(xmlFile, (List<XElement>)(Storage.RecordsetContainer), exportRecordCount);
        }
        #endregion

    }


    public class StmtExecutionResult
    {
        #region Private members
        private List<ScalarExecutionResult> m_ListScalarExecutionResult = new List<ScalarExecutionResult>();
        private List<RecordsetExecutionResult> m_ListRecordsetExecutionResult = new List<RecordsetExecutionResult>();
        #endregion

        #region Public property/methods
        public bool IsTargetResultset { get; set; }
        public string ErrorMessage { get; set; }
        public double ExecuteMilliseconds { get; set; }
        public double FetchDataMilliseconds { get; set; }
        public double TotalMilliseconds
        {
            get { return ExecuteMilliseconds + FetchDataMilliseconds; }
        }

        public void Reset()
        {
            IsTargetResultset = false;
            ErrorMessage = string.Empty;
            m_ListRecordsetExecutionResult.Clear();
            m_ListScalarExecutionResult.Clear();
            ExecuteMilliseconds = 0.0;
            FetchDataMilliseconds = 0.0;
        }

        public List<ScalarExecutionResult> ListScalarExecutionResult
        {
            get { return m_ListScalarExecutionResult; }
        }

        public List<RecordsetExecutionResult> ListRecordsetExecutionResult
        {
            get { return m_ListRecordsetExecutionResult; }
        }

        #endregion
    }
}
